package com.andyliu.sys.controller;

import com.andyliu.sys.dto.ResourceDto;
import com.andyliu.sys.dto.ResourceTableDto;
import com.andyliu.sys.pojo.SResource;
import com.andyliu.sys.pojo.SUser;
import com.andyliu.sys.service.ResourceService;
import com.andyliu.sys.service.UserService;
import com.andyliu.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: ResourceController
 * Description: TODO
 * date: 2021/1/20 5:19 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
@RestController
@RequestMapping("sys/resource")
@Api(tags = "资源相关请求",
produces ="application/json",
protocols = "http")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private UserService userService;
    @PostMapping("save")
    @ApiOperation(value = "保存系统资源",
            notes = "新增，修改资源信息",
            httpMethod = "POST")
    public Result saveResource(@ApiParam(name = "请求资源的相关信息",
            required = true,
            value = "属性序列化后的值") SResource resource) {
        Boolean status = resourceService.saveResource(resource);
        Result result = Result.ok(status);
        return result;
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询资源信息",
            notes = "根据id查询资源信息",
            httpMethod = "GET")
    public Result<Object> selectResource(@ApiParam(name = "id", value = "120", required = true) @PathVariable Integer id) {
        SResource resource = resourceService.selectResourceById(id);
        Result<Object> rs = Result.ok("",resource);
        return rs;
    }

    @PostMapping("query")
    @ApiOperation(notes = "资源列表查询",
            value = "资源树列表查询",
            httpMethod = "POST")
    public Result<Object> selectResources(
            @ApiParam(value = "模糊查询茶树") @RequestBody SResource resource) {
        List<ResourceTableDto> datas = resourceService.queryForTable(resource);
        Result<Object> rs = Result.ok("",datas);
        return rs;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(notes = "根据id删除资源信息",
            value = "根据id删除资源信息",
            httpMethod = "DELETE")
    public Result<Object> deleteResource(
            @ApiParam(name = "id", value = "需要删除的id", defaultValue = "688", required = true)
            @RequestParam(required = true) Integer id) {
        Boolean status = resourceService.deleteResourceById(id);
        Result<Object> rs = Result.ok(status);
        return rs;
    }

    @DeleteMapping("/deletes")
    @ApiOperation(notes = "批量删除",
            value = "根据id批量删除",
            httpMethod = "DELETE")
    public Result<Object> deleteResources(
            @ApiParam(name = "ids", value = "需要批量删除的id数组") Integer[] ids) {
        Boolean status = resourceService.deleteResourceById(ids);
        Result<Object> rs = Result.ok(status);
        return rs;
    }
    @ApiOperation(value = "查询所有资源",notes = "查询所有资源",produces = "application/json",protocols = "http")
    @GetMapping("/queryResources")
    public Result<Object> queryResources(@ApiParam(value = "用户id") Integer userId){
        SUser sUser = userService.queryUserById(userId);
        List<ResourceDto> resources = resourceService.selectResources(sUser.getRoleid());
        return Result.ok("",resources);
    }
}
